-- Handles the buying and upgrading backend processes

function inv_buycommand( player, command, arguments )
	local playerownedbuyables = glon.decode(player:GetOwnedBuyables())
	local ID = math.floor(arguments[1])
	
	print( "Player " .. player:GetName() .. " Attempting to buy ID " .. ID .. " (" .. INV_BUYABLES[ID][1] .. ")")
	
	if( player:Team() == TEAM_SPEC)then
		print( "Player is dead, aborting!")
		SendErrorMessageToPlayerChat(player, "You cannot buy stuff while dead!")
		return
	end
	
	-- Simple, Stop right here if the player is a skint doley
	if(INV_BUYABLES[ID][2] > player:GetMoney()) then
		-- Player can't afford this!
		--print("Player cannot afford!")
		SendErrorMessageToPlayerChat(player, "You cannot afford this!")
		return
	end
	
	if INV_BUYABLES[ID][7] != nil then
		-- Go through each pre
		for k, v in pairs(INV_BUYABLES[ID][7]) do
			-- Check if player has it
			-- print(v)
			if table.HasValue(playerownedbuyables, v) == false then
				SendErrorMessageToPlayerChat(player, "You can't buy this, you don't own the level(s) before it")
				return
			end
		end
	end
	
	if(INV_BUYABLES[ID][2] <= player:GetMoney()) then
		-- Melt Wallet
		player:DeductMoney(INV_BUYABLES[ID][2])
		
		-- Able to be bought once only?
		if(INV_BUYABLES[ID][6]) then
			player:AddOwnedBuyables(ID)
		end
		
		-- Some things aren't weapons, simple to handle this...
		if(INV_BUYABLES[ID][5] == "weapon") then
			player:Give(INV_BUYABLES[ID][3])
			--GivePlayerOwnedStuff(player) -- Reapply weapons...
			return
		end
		if(INV_BUYABLES[ID][5] == "function") then
			INV_BUYABLES[ID][3](player)
			return
		end
		if(INV_BUYABLES[ID][5] == "physical") then
			local tr = player:GetEyeTrace()
			local ent = ents.Create(INV_BUYABLES[ID][3])
			ent:SetPos(tr.HitPos)
			ent:Spawn()
			return
		end
		
		return
	end
	
end
concommand.Add( "inv_buy", inv_buycommand )

function inv_upgradespread( player, command, arguments )
	-- Check if the player can afford it
	if(player:GetStatpoints() < INV_SPREADUPGRADECOST[2]) then
		SendErrorMessageToPlayerChat(player, "You cannot afford this!")
		return
	end
	
	if((player:GetSpread() + INV_SPREADUPGRADECOST[1]) < INV_SPREADUPGRADECOST[3] or (player:GetSpread() + INV_SPREADUPGRADECOST[1]) > INV_SPREADUPGRADECOST[4]) then
		SendErrorMessageToPlayerChat(player, "This stat can not be upgraded any further!")
		return
	end
	
	if(player:GetStatpoints() >= INV_SPREADUPGRADECOST[2]) then
		player:DeductStatPoints(INV_SPREADUPGRADECOST[2])
		player:AddSpread(INV_SPREADUPGRADECOST[1])
	end
end
concommand.Add( "inv_upgradespread", inv_upgradespread )

function inv_upgradejump( player, command, arguments )
	-- Check if the player can afford it
	if(player:GetStatpoints() < INV_JUMPUPGRADECOST[2]) then
		SendErrorMessageToPlayerChat(player, "You cannot afford this!")
		return
	end
	if((player:GetJump() + INV_JUMPUPGRADECOST[1]) < INV_JUMPUPGRADECOST[3] or (player:GetJump() + INV_JUMPUPGRADECOST[1]) > INV_JUMPUPGRADECOST[4]) then
		SendErrorMessageToPlayerChat(player, "This stat can not be upgraded any further!")
		return
	end
	if(player:GetStatpoints() >= INV_JUMPUPGRADECOST[2]) then
		local oldweaponspeed = player:GetJump()
		
		player:DeductStatPoints(INV_JUMPUPGRADECOST[2])
		player:AddJump(INV_JUMPUPGRADECOST[1])
		
		player:SetJumpPower(INV_BASEJUMP * (player:GetJump() / 100))
		print(tostring(INV_BASEJUMP * (player:GetJump() / 100)))
	end
end
concommand.Add( "inv_upgradejump", inv_upgradejump )

function inv_upgradeweaponspeed( player, command, arguments )
	-- Check if the player can afford it
	if(player:GetStatpoints() < INV_WEAPONSPEEDUPGRADECOST[2]) then
		SendErrorMessageToPlayerChat(player, "You cannot afford this!")
		return
	end
	if((player:GetWeaponSpeed() + INV_WEAPONSPEEDUPGRADECOST[1]) < INV_WEAPONSPEEDUPGRADECOST[3] or (player:GetWeaponSpeed() + INV_WEAPONSPEEDUPGRADECOST[1]) > INV_WEAPONSPEEDUPGRADECOST[4]) then
		SendErrorMessageToPlayerChat(player, "This stat can not be upgraded any further!")
		return
	end
	if(player:GetStatpoints() >= INV_WEAPONSPEEDUPGRADECOST[2]) then
		local oldweaponspeed = player:GetWeaponSpeed()
		
		player:DeductStatPoints(INV_WEAPONSPEEDUPGRADECOST[2])
		player:AddWeaponSpeed(INV_WEAPONSPEEDUPGRADECOST[1])
		
		-- Update each weapon, yeah, this is hacky
		-- In a nutshell I calculate the DEFAULT speed by undoing
		-- the speed modifier then reapplying the new one from the default...
		--for k, v in pairs(player:GetWeapons()) do
			-- Set the weapon speed back to normal
			--v.Primary.Delay = v.Primary.Delay * (oldweaponspeed / 100)
			-- And now apply new speed
			--v.Primary.Delay = v.Primary.Delay / (player:GetWeaponSpeed() / 100)
		--end
	end
end
concommand.Add( "inv_upgradeweaponspeed", inv_upgradeweaponspeed )

function inv_upgradeweapondamage( player, command, arguments )
	-- Check if the player can afford it
	if(player:GetStatpoints() < INV_WEAPONDAMAGEUPGRADECOST[2]) then
		SendErrorMessageToPlayerChat(player, "You cannot afford this!")
		return
	end
	if((player:GetWeaponDamage() + INV_WEAPONDAMAGEUPGRADECOST[1]) < INV_WEAPONDAMAGEUPGRADECOST[3] or (player:GetWeaponDamage() + INV_WEAPONDAMAGEUPGRADECOST[1]) > INV_WEAPONDAMAGEUPGRADECOST[4]) then
		SendErrorMessageToPlayerChat(player, "This stat can not be upgraded any further!")
		return
	end
	if(player:GetStatpoints() >= INV_WEAPONDAMAGEUPGRADECOST[2]) then
		player:DeductStatPoints(INV_WEAPONDAMAGEUPGRADECOST[2])
		player:AddWeaponDamage(INV_WEAPONDAMAGEUPGRADECOST[1])
	end
end
concommand.Add( "inv_upgradeweapondamage", inv_upgradeweapondamage )

function inv_upgradeclipsize( player, command, arguments )
		-- Check if the player can afford it
	if(player:GetStatpoints() < INV_CLIPSIZEUPGRADECOST[2]) then
		SendErrorMessageToPlayerChat(player, "You cannot afford this!")
		return
	end
	if((player:GetClipSize() + INV_CLIPSIZEUPGRADECOST[1]) < INV_CLIPSIZEUPGRADECOST[3] or (player:GetClipSize() + INV_CLIPSIZEUPGRADECOST[1]) > INV_CLIPSIZEUPGRADECOST[4]) then
		SendErrorMessageToPlayerChat(player, "This stat can not be upgraded any further!")
		return
	end
	if(player:GetStatpoints() >= INV_CLIPSIZEUPGRADECOST[2]) then
		player:DeductStatPoints(INV_CLIPSIZEUPGRADECOST[2])
		player:AddClipSize(INV_CLIPSIZEUPGRADECOST[1])
	end
end
concommand.Add( "inv_upgradeclipsize", inv_upgradeclipsize )

function inv_upgradedamagetaken( player, command, arguments )
	-- Check if the player can afford it
	if(player:GetStatpoints() < INV_DAMAGETAKENUPGRADECOST[2]) then
		SendErrorMessageToPlayerChat(player, "You cannot afford this!")
		return
	end
	if((player:GetDamageTaken() + INV_DAMAGETAKENUPGRADECOST[1]) < INV_DAMAGETAKENUPGRADECOST[3] or (player:GetDamageTaken() + INV_DAMAGETAKENUPGRADECOST[1]) > INV_DAMAGETAKENUPGRADECOST[4]) then
		SendErrorMessageToPlayerChat(player, "This stat can not be upgraded any further!")
		return
	end
	if(player:GetStatpoints() >= INV_DAMAGETAKENUPGRADECOST[2]) then
		player:DeductStatPoints(INV_DAMAGETAKENUPGRADECOST[2])
		player:AddDamageTaken(INV_DAMAGETAKENUPGRADECOST[1])
	end
end
concommand.Add( "inv_upgradedamagetaken", inv_upgradedamagetaken )

function inv_upgrademaximumhealth( player, command, arguments )
	-- Check if the player can afford it
	if(player:GetStatpoints() < INV_MAXIMUMHEALTHUPGRADECOST[2]) then
		SendErrorMessageToPlayerChat(player, "You cannot afford this!")
		return
	end
	if((player:GetMaxHealth() + INV_MAXIMUMHEALTHUPGRADECOST[1]) < INV_MAXIMUMHEALTHUPGRADECOST[3] or (player:GetMaxHealth() + INV_MAXIMUMHEALTHUPGRADECOST[1]) > INV_MAXIMUMHEALTHUPGRADECOST[4]) then
		SendErrorMessageToPlayerChat(player, "This stat can not be upgraded any further!")
		return
	end
	if(player:GetStatpoints() >= INV_MAXIMUMHEALTHUPGRADECOST[2]) then
		player:DeductStatPoints(INV_MAXIMUMHEALTHUPGRADECOST[2])
		player:AddMaxHealth(INV_MAXIMUMHEALTHUPGRADECOST[1])
		-- Update the player so the changes take instant effect!
		player:SetMaxHealth(player:GetMaxHealth())
		-- They don't get given the health though...
	end
end
concommand.Add( "inv_upgrademaxhealth", inv_upgrademaximumhealth )

function inv_upgradespeed( player, command, arguments )
	-- Check if the player can afford it
	if(player:GetStatpoints() < INV_SPEEDUPGRADECOST[2]) then
		SendErrorMessageToPlayerChat(player, "You cannot afford this!")
		return
	end
	if((player:GetSpeed() + INV_SPEEDUPGRADECOST[1]) < INV_SPEEDUPGRADECOST[3] or (player:GetSpeed() + INV_SPEEDUPGRADECOST[1]) > INV_SPEEDUPGRADECOST[4]) then
		SendErrorMessageToPlayerChat(player, "This stat can not be upgraded any further!")
		return
	end
	if(player:GetStatpoints() >= INV_SPEEDUPGRADECOST[2]) then
		player:DeductStatPoints(INV_SPEEDUPGRADECOST[2])
		player:AddSpeed(INV_SPEEDUPGRADECOST[1])
		-- Update the player so the changes take instant effect!
		player:SetWalkSpeed(INV_BASESPEEDWALK * (player:GetSpeed() / 100))
		player:SetRunSpeed(INV_BASESPEEDRUN * (player:GetSpeed() / 100))
	end
end
concommand.Add( "inv_upgradespeed", inv_upgradespeed )